//
//  ByNetItemApi.m
//  jmsfhz_ios
//
//  Created by pro on 16/8/4.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ByNetItemApi.h"

#import "ByNetItemModel.h"              // 项目model

@implementation ByNetItemApi

/** 项目查询 */
+ (void)itemQuery:(ByNetParamsModel *)model
          success:(void(^)(ByNetBaseResult *result))success
          failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Project_query";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%.0lf%d", [NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.page_num forKey:@"page_num"];
    [params setObject:model.page_size forKey:@"page_size"];
    if (model.keyword) {
        [params setObject:model.keyword forKey:@"keyword"];
    }
    
    // 返回的responseObject 可能是字符串也可能是字典
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeDictionary
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           
           DLog(@"******** 项目获取 -> 成功: %@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"获取成功";
           ByNetItemModel *model = [[ByNetItemModel alloc] initContentWithDic:responseObject];
           result.data = model;
           result.notify_id = notifyID;

           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 项目获取 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert alertError:result.msg];
           }
       }];
}

/** 项目投标 */
+ (void)itemSend:(ByNetParamsModel *)model
         success:(void(^)(ByNetBaseResult *result))success
         failure:(void(^)(ByNetBaseResult *result))failure {
    
    static NSString *apiType = @"By_Project_apply";
    static NSInteger apiVersion = 100;
    int rand = arc4random() % 1000;
    
    NSString *requestId = [NSString stringWithFormat:@"%.0lf%d", [NSDate currentDateTimeInterval], rand];
    
    // 拼接参数
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    [params setObject:model.project_id forKey:@"project_id"];
    [params setObject:model.uid forKey:@"uid"];
    
    [self post:requestId
       apiType:apiType
      dataType:ByNetDataTypeString
     apiVerson:apiVersion
        params:params
       success:^(NSURLSessionDataTask *operation, id responseObject, NSString *notifyID) {
           NSLog(@"投标项目原始数据:%@", responseObject);
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = 0;
           result.msg = @"投标成功";
           result.data = responseObject;
           result.notify_id = notifyID;

           if(success) {
               success(result);
           }
           
       } failure:^(NSURLSessionDataTask *operation, NSError *error){
           
           DLog(@"******** 投标 -> 失败: %@", error);
           
           ByNetBaseResult *result = [[ByNetBaseResult alloc] init];
           result.code = error.code;
           result.msg = error.domain;
           result.data = error.localizedDescription;
           if(failure) {
               failure(result);
           } else {
               [ByCoreToolAlert alertError:result.msg];
           }
       }];
}
@end
